package com.dji.activationDemo.messaging;

import android.os.Handler;

import com.dji.activationDemo.DemoApplication;
import com.dji.activationDemo.uitls.RandomNum;
import com.dji.activationDemo.websocket.WsManager;

import dji.common.camera.SettingsDefinitions;
import dji.common.error.DJIError;
import dji.common.product.Model;
import dji.common.util.CommonCallbacks;
import dji.sdk.base.BaseProduct;
import dji.sdk.sdkmanager.DJISDKManager;

public class Camera {
    private Handler handler = new Handler();
    RandomNum randomNum = new RandomNum();
    private String liveShowUrl = "rtmp://58.34.215.18:1004/Live/wrj";

    public void liveStream(String sign) {
        if (!isLiveStreamManagerOn()) {
            WsManager.ws.sendText("error*" + sign + "*没有流管理,尝试重新启动app");
            return;
        }
        if (DJISDKManager.getInstance().getLiveStreamManager().isStreaming()) {
            WsManager.ws.sendText("error*" + sign + "*已经开启直播了");
            System.out.println("已经开启直播了");
            return;
        }
        new Thread() {
            @Override
            public void run() {
                DJISDKManager.getInstance().getLiveStreamManager().setLiveUrl(liveShowUrl);
                int result = DJISDKManager.getInstance().getLiveStreamManager().startStream();
//                DJISDKManager.getInstance().getLiveStreamManager().setStartTime();
            }
        }.start();
        WsManager.ws.sendText("received*" + sign);
    }

    private boolean isLiveStreamManagerOn() {
        if (DJISDKManager.getInstance().getLiveStreamManager() == null) {
            return false;
        }
        return true;
    }

    public void stopLiveShow(String sign) {
        if (!isLiveStreamManagerOn()) {
            WsManager.ws.sendText("error*" + sign + "*没有流管理,尝试重新启动app");
            return;
        }
        DJISDKManager.getInstance().getLiveStreamManager().stopStream();
        WsManager.ws.sendText("received*" + sign);
    }


    //拍照
    public void shootPhoto(String sign, String sendName) {
        final dji.sdk.camera.Camera camera = DemoApplication.getCameraInstance();
        if (camera != null) {
            if (isMavicAir2() || isM300()) {
                camera.setFlatMode(SettingsDefinitions.FlatCameraMode.PHOTO_SINGLE, djiError -> {
                    if (null == djiError) {
                        takePhoto(sign, sendName);
                    } else {
                        sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"相机模式切换报错：" + djiError.getDescription() + "\"}}");
//                        WsManager.ws.sendText("error*" + sign + "*相机模式切换报错：" + djiError);
                    }
                });
            } else {
                camera.setShootPhotoMode(SettingsDefinitions.ShootPhotoMode.SINGLE, djiError -> {
                    if (null == djiError) {
                        takePhoto(sign, sendName);
                    } else {
                        sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"相机模式切换报错：" + djiError.getDescription() + "\"}}");
//                        WsManager.ws.sendText("error*" + sign + "*相机模式切换报错：" + djiError);
                    }
                });
            }
        }
    }

    private void takePhoto(String sign, String sendName) {
        final dji.sdk.camera.Camera camera = DemoApplication.getCameraInstance();
        if (camera == null) {
            sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"请手动重启app\"}}");
//            WsManager.ws.sendText("error*" + sign + "*手动重启app：");
            return;
        }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                camera.startShootPhoto(djiError -> {
                    if (djiError == null) {
                        sendMessage(sendName + "*" + sign + "*0*{code:\"received\"}");
//                        WsManager.ws.sendText("received*" + sign);
                    } else {
                        sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"拍照报错：" + djiError.getDescription() + "\"}}");
//                        WsManager.ws.sendText("error*" + sign + "*拍照报错：" + djiError);
                    }
                });
            }
        }, 2000);
    }

    //开始录像
    public void startRecord(String sign, String sendName) {
        try {
            if (isMavicAir2() || isM300()) {
                switchCameraFlatMode(SettingsDefinitions.FlatCameraMode.VIDEO_NORMAL, sign, sendName);
            } else {
                switchCameraMode(SettingsDefinitions.CameraMode.RECORD_VIDEO, sign, sendName);
            }
            Thread.sleep(2000);
            final dji.sdk.camera.Camera camera = DemoApplication.getCameraInstance();
            if (camera != null) {
                camera.startRecordVideo(djiError -> {
                    if (djiError == null) {
                        sendMessage(sendName + "*" + sign + "*0*{code:\"received\"}");
//                        WsManager.ws.sendText("received*" + sign);
                    } else {
                        sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"录像报错：" + djiError.getDescription() + "\"}}");
//                        WsManager.ws.sendText("error*" + sign + "*录像报错：" + djiError.getDescription());
                    }
                }); // Execute the startRecordVideo API
            }
        } catch (Exception e) {
            sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"录像报错：" + e.toString() + "\"}}");
//            WsManager.ws.sendText("error*" + sign + "*录像报错：" + e.toString());
        }
    }

    //停止录像
    public void stopRecord(String sign, String sendName) {
        dji.sdk.camera.Camera camera = DemoApplication.getCameraInstance();
        if (camera != null) {
            camera.stopRecordVideo(djiError -> {
                if (djiError == null) {
                    sendMessage(sendName + "*" + sign + "*0*{code:\"received\"}");

//                    WsManager.ws.sendText("received*" + sign);
                } else {
                    sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"停止录像报错：" + djiError.getDescription() + "\"}}");
//                    WsManager.ws.sendText("error*" + sign + "*停止录像报错：" + djiError.getDescription());
                }
            }); // Execute the stopRecordVideo API
        }
    }

    private void switchCameraFlatMode(SettingsDefinitions.FlatCameraMode flatCameraMode, String sign, String sendName) {
        dji.sdk.camera.Camera camera = DemoApplication.getCameraInstance();
        if (camera != null) {
            camera.setFlatMode(flatCameraMode, error -> {
                if (error != null) {
                    sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"执行报错：" + error.getDescription() + "\"}}");
//                    WsManager.ws.sendText("error*" + sign + "*相机模式切换报错：" + error.getDescription());
                }
            });
        }
    }

    private void switchCameraMode(SettingsDefinitions.CameraMode cameraMode, String sign, String sendName) {
        dji.sdk.camera.Camera camera = DemoApplication.getCameraInstance();
        if (camera != null) {
            camera.setMode(cameraMode, error -> {
                if (error != null) {
                    sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"执行报错：" + error.getDescription() + "\"}}");
//                    WsManager.ws.sendText("error*" + sign + "*相机模式切换报错：" + error.getDescription());
                }
            });
        }
    }

    private boolean isMavicAir2() {
        BaseProduct baseProduct = DemoApplication.getProductInstance();
        if (baseProduct != null) {
            return baseProduct.getModel() == Model.MAVIC_AIR_2;
        }
        return false;
    }

    private boolean isM300() {
        BaseProduct baseProduct = DemoApplication.getProductInstance();
        if (baseProduct != null) {
            return baseProduct.getModel() == Model.MATRICE_300_RTK;
        }
        return false;
    }


    //发送消息
    public void sendMessage(String message) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean start = true;
                while (start) {
                    try {
                        if (WsManager.ws.isOpen()) {
                            System.out.println("发送成功：" + message);
                            WsManager.ws.sendText(message);
                            start = false;
                        }
                        Thread.sleep(250);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();
    }
}
